package business.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import model.TRole;
import business.basic.iHibBaseDAO;
import business.basic.iHibBaseDAOImpl;
import business.dao.RoleDAO;

@Component("roledao")
public class RoleDAOImpl implements RoleDAO {
	
	private iHibBaseDAO bdao = null;

	public RoleDAOImpl() {
		bdao = new iHibBaseDAOImpl();
	}

	public void setBdao(iHibBaseDAO bdao) {
		this.bdao = bdao;
	}

	@Override
	public int add(TRole role) {
		Object obj = bdao.insert(role);
		if(obj!=null){
			return Integer.parseInt(obj.toString());
		}
		return 0;
	}

	@Override
	public boolean update(TRole role) {
		return bdao.update(role);
	}

	@Override
	public boolean delete(int roleid) {
		return bdao.delete(TRole.class, roleid);
	}

	@Override
	public TRole getTRoleById(int roleid) {
		return (TRole)bdao.findById(TRole.class, roleid);
	}

	@Override
	public List<TRole> getTRole() {
		String hql = "from TRole";
		return bdao.select(hql);
	}

	@Override
	public List<TRole> getTRoleByWhere(String wherecondition, int startPage,
			int pageSize) {
		String hql = "from TRole";
		if(wherecondition!=null && !wherecondition.equals("")){
			hql += wherecondition;
		}
		return bdao.selectByPage(hql, startPage, pageSize);
	}

	@Override
	public int getAmounTRoleByWhere(String wherecondition) {
		String hql = "select count(*) from TRole ";
		if (wherecondition != null && !wherecondition.equals("")) {
			hql += wherecondition;
		}
		return bdao.selectValue(hql);
	}

}
